//
//  SearchGroupViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/1.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "SearchGroupViewController.h"
#import "GroupMessageTableViewCell.h"
#import "CeShiModelOfGroupMessage.h"
#import "GroupDetailViewController.h"


@interface SearchGroupViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UISearchBar *mySearchBar;
@property (nonatomic, strong) NSMutableArray *searchGroupArray; // 搜索小组数据源
@property (nonatomic, strong) UITableView *groupTableview; // 好友tableview

@end

@implementation SearchGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // navi的修改
    [self changeNavigation];
    // 添加searchBar
    [self addSearchBar];
    // 添加小组tableview
    [self addGroupTableview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}



#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 添加searchBar
- (void)addSearchBar
{
    self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260 * kAdjustParamater, 30 * kAdjustParamater)];
    self.mySearchBar.barStyle = UIBarStyleDefault;
    self.mySearchBar.delegate = self;
    self.mySearchBar.placeholder = @"输入小组的名字/ID";
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260 * kAdjustParamater, 30 * kAdjustParamater)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [view addSubview:_mySearchBar];
    self.navigationItem.titleView = view;
}

#pragma mark - UISearchBar代理

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"searchText = %@", searchText);
    self.searchGroupArray = [NSMutableArray array];
    NSDictionary *dicGroup1 = @{@"_groupImage": @"", @"_groupName": @"心脑血管", @"_introduceGroup": @"该群组是对于心脑血管建立的一个兴趣小组", @"_mark": @[@"健康小组", @"爱看电影的小组"]};
    NSDictionary *dicGroup2 = @{@"_groupImage": @"", @"_groupName": @"心脏病", @"_introduceGroup": @"该群组是对于心脏病的一个兴趣小组", @"_mark": @[@"爱出门运动的小组", @"运动小组"]};
    NSArray *tempArray = [NSArray arrayWithObjects:dicGroup1, dicGroup2, nil];
    for (NSDictionary *tempDic in tempArray) {
        if ([[tempDic valueForKey:@"_groupName"] containsString:searchText]) {
            CeShiModelOfGroupMessage *tempModel = [[CeShiModelOfGroupMessage alloc] init];
            [tempModel setValuesForKeysWithDictionary:tempDic];
            [self.searchGroupArray addObject:tempModel];
        }
    }
    [self.groupTableview reloadData];
}

#pragma mark - 添加小组tableview
- (void)addGroupTableview
{
    self.groupTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.groupTableview.dataSource = self;
    self.groupTableview.delegate = self;
    [self.view addSubview:_groupTableview];
}

// tableview代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.searchGroupArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    // 这里cell不能服用
    static NSString *identifier = @"groupMessage";
    GroupMessageTableViewCell *cell = [[GroupMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    // 获取model
    cell.model = self.searchGroupArray[indexPath.row];
    [cell.attentionGroupButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.attentionGroupButton.tag = 205;
    [cell.addGroupButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.addGroupButton.tag = 206;
    return cell;
}

#pragma mark - button的点击事件
- (void)clickButton:(UIButton *)button
{
    UIView *v = [button superview];
    GroupMessageTableViewCell *cell = (GroupMessageTableViewCell *)[v superview];
    NSIndexPath *tempIndexPath = [self.groupTableview indexPathForCell:cell];
    switch (button.tag) {
        case 205:
        {
            // 说明是关注小组
            NSLog(@"关注小组, row = %ld", tempIndexPath.row);
            break;
        }
        case 206:
        {
            // 说明是申请入组
            NSLog(@"申请入组,row = %ld", tempIndexPath.row);
            break;
        }
        default:
            break;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSLog(@"indexpath = %ld", indexPath.row);
    GroupDetailViewController *groupDetail = [[GroupDetailViewController alloc] init];
    [self.navigationController pushViewController:groupDetail animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75 * kAdjustParamater;
}

// 取消键盘
-  (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
